{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 收益率概念与平稳性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 资产收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多数金融研究针对的是资产收益率而不是资产价格。\n",
    "\n",
    "使用收益率有两个主要理由：\n",
    "1. 对普通的投资者来说，资产收益率完全体现了该资产的投资机会，且与其投资规模无关；\n",
    "2. 收益率序列比价格序列更容易处理，因为前者有更好的统计性质\n",
    "\n",
    "然而，资产收益率有多种定义，设 $P_t$ 是资产在 $t$ 时刻的价格。下面给出常见的几个收益率定义.暂时假定资产不支付分红。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 单期简单收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "若从第 $t-1$ 天到第 $t$ 天(一个周期)持有某种资产，则**简单毛收益率**为：\n",
    "\n",
    "$$1+R_{t}=\\frac{P_{t}}{P_{t-1}} \\quad \\text { 或 } \\quad P_{t}=P_{t-1}\\left(1+R_{t}\\right)\\tag{1}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对应的**单期简单收益率**或称**简单收益率**为：\n",
    "\n",
    "$$R_{t}=\\frac{P_{t}}{P_{t-1}}-1=\\frac{P_{t}-P_{t-1}}{P_{t-1}}\\tag{2}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 连续复合收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "资产的简单毛收益率的自然对数称为**连续复合收益率** 或 **对数收益率**：\n",
    "\n",
    "$$r_{t}=\\ln \\left(1+R_{t}\\right)=\\ln \\frac{P_{t}}{P_{t-1}}=p_{t}-p_{t-1}\\tag{3}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其中 $p_{t}=\\ln P_{t}$，与简单净收益率 $R_t$ 相比，连续复合收益率 $r_t$ 有一些优点。首先，对多期收益率，我们有：\n",
    "\n",
    "$$\\begin{aligned}\n",
    "r_{t}[k] &=\\ln \\left(1+R_{t}[k]\\right)=\\ln \\left[\\left(1+R_{t}\\right)\\left(1+R_{t-1}\\right) \\cdots\\left(1+R_{t-k+1}\\right)\\right] \\\\\n",
    "&=\\ln \\left(1+R_{t}\\right)+\\ln \\left(1+R_{t-1}\\right)+\\cdots+\\ln \\left(1+R_{t-k+1}\\right) \\\\\n",
    "&=r_{t}+r_{t-1}+\\cdots+r_{t-k+1}\n",
    "\\end{aligned}\\tag{4}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这样，连续复合多期收益率就是它所包含的连续复合单期收益率之和。其次，对数收益率具有更容易处理的统计性质。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>hs300</th>\n",
       "      <th>sz</th>\n",
       "      <th>sz收益率</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-11-21</td>\n",
       "      <td>3889.5980</td>\n",
       "      <td>2903.6379</td>\n",
       "      <td>0.633954</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-11-20</td>\n",
       "      <td>3907.8641</td>\n",
       "      <td>2911.0534</td>\n",
       "      <td>0.255061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-11-19</td>\n",
       "      <td>3947.0392</td>\n",
       "      <td>2933.9908</td>\n",
       "      <td>0.784854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-11-18</td>\n",
       "      <td>3907.9291</td>\n",
       "      <td>2909.2002</td>\n",
       "      <td>-0.848535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-11-15</td>\n",
       "      <td>3877.0892</td>\n",
       "      <td>2891.3431</td>\n",
       "      <td>-0.615706</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期      hs300         sz     sz收益率\n",
       "0 2019-11-21  3889.5980  2903.6379  0.633954\n",
       "1 2019-11-20  3907.8641  2911.0534  0.255061\n",
       "2 2019-11-19  3947.0392  2933.9908  0.784854\n",
       "3 2019-11-18  3907.9291  2909.2002 -0.848535\n",
       "4 2019-11-15  3877.0892  2891.3431 -0.615706"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "stock = pd.read_excel('../数据/上证指数与沪深300.xlsx')\n",
    "stock['sz收益率'] = 100*np.log(stock['sz']/stock['sz'].shift(1))\n",
    "stock = stock.dropna()   #删除缺失值\n",
    "stock = stock.reset_index(drop=True)\n",
    "stock.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 平稳性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "平稳性是时间序列分析的基础。\n",
    "\n",
    "时间序列 {$r_t$} 称为**严平稳**的(strictly stationary)，如果对所有的 $t$，任意正整数 $k$ 和任意 $k$ 个正整数($t_1,\\cdots,t_k$)，($r_{t_1},\\cdots,r_{t_k}$)的联合分布与($r_{t_1+t},\\cdots,r_{t_k+t}$)的联合分布是相同的.换言之，严平稳性要求($r_{t_1},\\cdots,r_{t_k}$)的联合分布在时间的平移变换下保持不变。这是一个很强的条件，难以用经验方法验证，经常假定的是平稳性的一一个较弱的形式。\n",
    "\n",
    "时间序列 {$r_t$} 称为**弱平稳**的（weakly stationary），如果 $r_t$ 的均值与 $r_t$ 和 $r_{t-l}$ 的协方差不随时间而改变，其中 $l$ 是任意整数.更具体地说，{$r_t$} 是弱平稳的，若：\n",
    "1. $E_{r_t}=\\mu$，$\\mu$ 是一个常数；\n",
    "2. $\\operatorname{Cov}\\left(r_{t}, r_{t-l}\\right)=\\gamma_{l}$，$\\gamma_{l}$ 只依赖于 $l$。\n",
    "\n",
    "在实际中，假定我们有 $T$ 个数据观测点$\\left\\{r_{t} | t=1, \\cdots, T\\right\\}$，弱平稳性意味着数据的时间图显示出 $T$ 个值在一个常数水平上下以相同幅度波动。在应用中，弱平稳性使我们可以对未来观测进行推断，即预测。\n",
    "\n",
    "在弱平稳性的条件中，我们隐含地假定了 $r_t$ 的前两阶矩是有限的.由定义可见，若 $r_t$ 是严平稳的且它的前两阶矩是有限的，则 $r_t$ 也是弱平稳的.反之，一般是不成立的.但如果时间序列 $r_t$ 是正态分布的，则弱平稳性与严平稳性是等价的.本内容主要考虑弱平稳序列。\n",
    "\n",
    "协方差 $\\gamma_{l}=\\operatorname{Cov}\\left(r_{t}, r_{t-l}\\right)$ 称为 $r_t$ 的间隔为 $l$ 的自协方差、它具有两个重要性质：\n",
    "1. $\\gamma_{0}=\\operatorname{Var}\\left(r_{t}\\right)$\n",
    "2. $\\gamma_{-l}=\\gamma_{l}$\n",
    "\n",
    "第二个性质成立是因为$\\operatorname{Cov}\\left(r_{t}, r_{t-(-l)}\\right)=\\operatorname{Cov}\\left(r_{t-(-l)}, r_{t}\\right)=\\operatorname{Cov}\\left(r_{t+l}, r_{t}\\right)=\\operatorname{Cov}\\left(r_{t_{1}}, r_{t_{1}-l}\\right)$，其中$t_{1}=t+l$\n",
    "\n",
    "在金融文献中，通常假定资产收益率序列是弱平稳的.只要有足够多的历史收益率数据，这个假定可以用实证方法验证，例如，我们可以把数据分成若干子样本，然后检验它们的一致性。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
